package com.agent.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.agent.exception.MyException;
import com.agent.exception.StaticExceptionCodes;
import com.agent.mapper.AgentMapper;
import com.agent.model.Agent;
import com.agent.service.IAgentSer;
import com.agent.tool.StringTools;

@Service("agentSer")
public class AgentSerImpl implements IAgentSer{
	@Autowired
	AgentMapper agentMapper;
	
	/*添加代理*/
	public int addAgent(Agent agent,Integer level,Integer agentid) throws MyException {
		if(agent.getAgentname()==null||agent.getAccount()==null||agent.getPassword()==null){
			throw new MyException(StaticExceptionCodes.CommonException.messageLack);
		}
		/*判断代理等级*/
		if(level==0){
			agent.setLevel(1);
		}
		if(level==1){
			agent.setLevel(2);
		}
		if(level==2){
			throw new MyException(StaticExceptionCodes.AgentException.insufficientPrivilege);
		}
		/*被谁创建*/
		agent.setCreateBy(agentid);
		/*代理码*/
		agent.setAegntCode(StringTools.getRandomString(18));
		int result = agentMapper.addAgent(agent);
		if(result<=0){
			throw new MyException(StaticExceptionCodes.AgentException.addAgentFail);
		}
		return result;
	}
	
	/*代理登录*/
	public Agent loginAgent(Agent agent) throws MyException {
		if(agent.getAccount()==null||agent.getPassword()==null){
			throw new MyException(StaticExceptionCodes.CommonException.messageLack);
		}
		Agent result = agentMapper.loginAgent(agent);
		if(result==null){
			throw new MyException(StaticExceptionCodes.AgentException.loginFail);
		}
		return result;
	}

}
